<?php

namespace App\Logics;


use Throwable;
use App\Service\Captcha\ClickCaptcha;


class CaptchaLogic extends Logic
{
    /**
     * 图形验证码
     * @throws Throwable
     */
    public function captcha()
    {
        $opt    = $this->option();
        $cId    = $opt->getData('id', null, 'trim');
        $config = array(
            'codeSet'  => '123456789',            // 验证码字符集合
            'fontSize' => 22,                     // 验证码字体大小(px)
            'useCurve' => false,                  // 是否画混淆曲线
            'useNoise' => true,                   // 是否添加杂点
            'length'   => 4,                      // 验证码位数
            'bg'       => array(255, 255, 255),   // 背景颜色
        );

        $captcha = new Captcha($config);

        return $captcha->entry($cId);
    }

    /**
     * 生成点选验证码
     */
    public function clickCaptcha()
    {
        $opt = $this->option();
        $cId = $opt->getData('id', null, 'trim');
        //$cId     = 'f74296c9-8932-489d-ad79-e65473f36de8';
        $captcha = new ClickCaptcha();
        $res     = $captcha->create($cId);

        return $res;
    }

    /**
     * 点选验证码检查：验证
     * @throws Throwable
     */
    public function checkClickCaptcha()
    {
        $opt   = $this->option();
        $cId   = $opt->getData('id', null, 'trim');
        $info  = $opt->getData('info', null, 'trim');
        $unset = $opt->getData('unset', null, 'trim');
        $is_ok = (new ClickCaptcha())->check($cId, $info, $unset);
        if(empty($is_ok)){
            return $this->_returnFail('verification_code_is_wrong');
        }

        return $is_ok;
    }

}
